package com.egg.ylt.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.Utils.TimeCountUtil;
import com.egg.ylt.presenter.impl.NewPhonePresenterImpl;
import com.egg.ylt.view.INewPhoneView;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;
import com.yonyou.framework.library.common.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_SetNewPhone extends BaseActivity<NewPhonePresenterImpl> implements INewPhoneView {

    @BindView(R.id.include_rl_view)
    RelativeLayout mIncludeRlView;
    @BindView(R.id.include_iv_back)
    ImageView includeIvBack;
    @BindView(R.id.include_tv_title)
    TextView includeTvTitle;
    @BindView(R.id.edit_new_phone)
    EditText editNewPhone;
    @BindView(R.id.edit_update_code)
    EditText editUpdateCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.btn_new_next)
    TextView btnNewNext;
    @BindView(R.id.target_view_ll)
    LinearLayout llTargetView;

    private TimeCountUtil mTime;
    private AppSharedPreferences mSp;
    private String mPhone;

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_set_new_phone;
    }

    @Override
    protected View getLoadingTargetView() {
        return llTargetView;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, mIncludeRlView);

        //设置title
        includeTvTitle.setText("更换手机号");

        mSp = new AppSharedPreferences(mContext);

        //设置倒计时参数
        mTime = new TimeCountUtil(60000, 1000, mContext, tvCode);
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @OnClick({R.id.include_iv_back, R.id.btn_new_next, R.id.tv_code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.include_iv_back://返回键
                finish();
                break;
            case R.id.btn_new_next://完成
                mPhone = editNewPhone.getText().toString().trim();
                if (!StringUtil.isPhone(mPhone)) {
                    ToastUtil.makeText(mContext, "请检查手机号", false);
                    return;
                }
                String verificationCode = editUpdateCode.getText().toString().trim();
                if (verificationCode.length() < 6 && StringUtil.isEmpty(verificationCode)) {
                    ToastUtil.makeText(mContext, "请检查验证码是否正确", false);
                    return;
                }
                mPresenter.savePhone(mPhone, Constants.COMPANYID, Constants.TOKEN, verificationCode);
                break;
            case R.id.tv_code://发送验证码
                mPhone = editNewPhone.getText().toString().trim();
                if (!StringUtil.isPhone(mPhone)) {
                    ToastUtil.makeText(mContext, "请检查手机号", false);
                    return;
                }
                mPresenter.checkedPhone(mPhone, Constants.COMPANYID, "0");
                break;
        }
    }

    @Override
    public void saveNewPhone() {
        mSp.putString(Constants.USER_PHONE, mPhone);
        ToastUtil.makeText(mContext, "修改手机号成功", false);
        readyGo(ACT_UserSetting.class);
    }

    @Override
    public void getPhoneCode() {
        mPresenter.getCode(mPhone);
        mTime.start();
    }

    @Override
    public void showError(String msg) {
        super.showError(msg);
        ToastUtil.makeText(mContext, msg, false);
    }
}
